<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class AdminsController extends AdminController{

	public function index(){
	    $where = 'a.gid=g.id';
		$where .= I('get.gid',-1) > 0 ? ' and a.gid=' . I('get.gid', 0, 'intval') : '';
		$where .= I('get.name') ? ' and a.name like "%' . I('get.name') . '%"' : '';
		$where .= I('get.gh') ? ' and a.gh like "%' . I('get.gh') . '%"' : '';
		$where .= I('get.sysxx_id', -1) > -1 ? ' and a.sysxx_id =' . I('get.sysxx_id') : '';
		$where .= I('get.dep_id', -1) > -1 ? ' and a.dep_id =' . I('get.dep_id') : '';
		$list = $this->getList('a.id,a.sysxx_name,a.dep_name,a.name,a.status,a.gh,a.gid,g.name as groupname',
				array('group g', 'admins a'),
				$where, 'id desc', true,10);
		$this->assign('list', $list);
		$sysxxList = $this->getList('id,name', 'js_base',"type = 'sysxx'");
		$this->assign('sysxxList', $sysxxList);

		$depList = $this->getList('id,pid,name', 'dep', "pid != 0", 'sort desc,id asc');
		$this->assign('depList', $depList);

		$groupList = $this->getList('id,name', 'group');
		$this->assign('groupList', $groupList);

		$this->display();
	}

	public function add(){
		if(IS_POST){
			$_POST['pwd'] = md5($_POST['name'] . '_NiU_' . $_POST['pwd']);
			$result = $this->updateData($_POST, 'admins');
			$this->returnResult($result);
		}
	}

	public function edit(){
		if(IS_POST){
			$_POST['status'] = $_POST['set_status'];
			unset($_POST['set_status']);
			if(!empty($_POST['pwd'])){
				$_POST['pwd'] = md5($_POST['name'] . '_NiU_' . $_POST['pwd']);
			}else{
				unset($_POST['pwd']);
			}
			$result = $this->updateData($_POST, 'admins',2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('*', 'admins', array('id' => I('get.id', 0, 'intval')));
			if($info){
				$data['info'] = $info;
				$data['status'] = 1;
			}else{
				$data['info'] = '数据不存在';
				$data['status'] = 0;
			}
			$this->ajaxReturn($data);

			
		}
	}

	public function del(){
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'admins');
		$this->returnResult($result);
	}
}